package com.itheima.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录过滤器
 */
@Slf4j
//@WebFilter(filterName = "loginCheckFilter", urlPatterns = "/*")
@WebFilter("/*")
public class LoginCheckFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest  request=(HttpServletRequest)servletRequest;
        HttpServletResponse response=(HttpServletResponse)servletResponse;
        String requestURI = request.getRequestURI();//获取本次请求
        log.info("拦截到路径{}",requestURI);
        String[] urls = new String[]{
                "/backend/**",
                "/front/**",
                "/employee/login",
                "/employee/logout",
                "/common/**",
                "/user/login",
                "/category/list",
                "/setmeal/list",
                "/dish/list",
                "/shoppingCart/add",
                "/addressBook/**"


//                "user/sendMsg"
        };
        if (check(urls,requestURI)){

            filterChain.doFilter(request,response);//白名单 放行
            return;

        }

        Integer id = (Integer) request.getSession().getAttribute("id");
        if (id == null){
            //未登录
         //  response.getWriter().write( JSON.toJSONString(R.error("NOTLOGIN")));//拦截
        }
        else{
            //已登录
            filterChain.doFilter(request,response);//放行
            return;
        }



    }

    public boolean check(String[] urls,String requestURI)  {
       AntPathMatcher pathMatcher = new AntPathMatcher();

        for (String url : urls) {
            //判断当当前请求路径是否存在白名单
            if (pathMatcher.match(url,requestURI)){
              return  true;
            }

        }
        return false;
    }


//    @Override
//    public void init(FilterConfig filterConfig) throws ServletException {
//        Filter.super.init(filterConfig);
//    }
//
//    @Override
//    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//        HttpServletRequest  request=(HttpServletRequest)servletRequest;
//        HttpServletResponse response=(HttpServletResponse)servletResponse;
//        String requestURI = request.getRequestURI();//获取本次请求
//        log.info("拦截到路径{}",requestURI);
//        String[] url = new String[]{
//                "/backend/**",
//                "/front/**",
//                "/employee/login",
//                "/employee/logout",
//        };
//        boolean check = this.check(url, requestURI);
//        if(check){
//            filterChain.doFilter(servletRequest,servletResponse);
//            return;
//        }
//        if(request.getSession().getAttribute("id") != null){
//            filterChain.doFilter(servletRequest,servletResponse);
//            return;
//        }
//        response.getWriter().write("成功");
//        return;
//
//
//    }
//    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();
//    public boolean check(String[] urls,String requestURI){
//        for (String url : urls) {
//            if(PATH_MATCHER.match(url,requestURI)){
//                return true;
//            }
//        }
//        return false;
//    }
//
//
//    @Override
//    public void destroy() {
//        Filter.super.destroy();
//    }
}
